package leetcode.f1t100;

/**
 * 反转整数
 *
 * https://leetcode.com/problems/reverse-integer/
 *
 * @author lichx
 * @date 2021/9/24 17:14
 */
public class Q7_ReverseInteger {
    public int reverse(int x) {
        int ans = 0;
        while (x / 10 != 0) {
            ans = ans * 10 + x % 10;
            x = x / 10;
            if((x>0 && ((Integer.MAX_VALUE - x % 10) / 10 < ans)) || (x<0&&((Integer.MIN_VALUE - x % 10) / 10 > ans))){
                return 0;
            }
        }
        ans = ans * 10 + x % 10;
        return ans;
    }

    public static void main(String[] args) {
        Q7_ReverseInteger a = new Q7_ReverseInteger();
        System.out.println(a.reverse(-2147483612));
        System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.MAX_VALUE);
    }
}
